﻿using System;
using System.Linq;

namespace K9Nano.Dependency
{
    /// <summary>
    /// Find the service type by name: The name of service type is a prefix of the name of implementation type.
    /// </summary>
    public class DefaultDependencyConvention : IDependencyConvention
    {
        public Type[] GetServiceTypes(Type implementationType)
        {
            return implementationType.GetInterfaces()
                .Where(x => implementationType.Name.EndsWith(GetConventionalName(x.Name))
                        )
                .ToArray();
        }

        protected virtual string GetConventionalName(string interfaceName)
        {
            return interfaceName.StartsWith('I') ? interfaceName.Substring(1) : interfaceName;
        }
    }
}